package io.cici.cc.mybatis.lite.reflection.wrapper;

import io.cici.cc.mybatis.lite.reflection.MetaDataObject;
import io.cici.cc.mybatis.lite.reflection.PropertyTokenizer;
import io.cici.cc.mybatis.lite.reflection.SystemMetaObject;
import io.cici.cc.mybatis.lite.reflection.factory.ObjectFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MapWrapper extends BaseWrapper {

    private final Map<String, Object> map;

    public MapWrapper(MetaDataObject metaDataObject, Map<String, Object> map) {
        super(metaDataObject);
        this.map = map;
    }

    @Override
    public Object get(PropertyTokenizer prop) {
        if (prop.getIndex() != null) {
            Object collection = resolveCollection(prop, map);
            return getCollectionValue(prop, collection);
        }
        return map.get(prop.getName());
    }

    @Override
    public void set(PropertyTokenizer prop, Object value) {
        if (prop.getIndex() != null) {
            Object collection = resolveCollection(prop, map);
            setCollectionValue(prop, collection, value);
        } else {
            map.put(prop.getName(), value);
        }
    }


    @Override
    public Class<?> getSetterType(String name) {
        PropertyTokenizer propertyTokenizer = new PropertyTokenizer(name);
        if (propertyTokenizer.hasNext()) {
            MetaDataObject metaValue = metaDataObject.metaObjectForProperty(propertyTokenizer.getNameAndIndex());
            if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
                return Object.class;
            } else {
                return metaValue.getSetterType(propertyTokenizer.getNext());
            }
        }
        if (map.get(name) != null) {
            return map.get(name).getClass();
        } else {
            return Object.class;
        }
    }

    @Override
    public Class<?> getGetterType(String name) {
        PropertyTokenizer propertyTokenizer = new PropertyTokenizer(name);
        if (propertyTokenizer.hasNext()) {
            MetaDataObject metaValue = metaDataObject.metaObjectForProperty(propertyTokenizer.getNameAndIndex());
            if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
                return Object.class;
            } else {
                return metaValue.getGetterType(propertyTokenizer.getNext());
            }
        }
        if (map.get(name) != null) {
            return map.get(name).getClass();
        } else {
            return Object.class;
        }
    }

    @Override
    public boolean hasSetter(String name) {
        return true;
    }

    @Override
    public boolean hasGetter(String name) {
        PropertyTokenizer propertyTokenizer = new PropertyTokenizer(name);
        if (!propertyTokenizer.hasNext()) {
            return map.containsKey(propertyTokenizer.getName());
        }
        if (map.containsKey(propertyTokenizer.getNameAndIndex())) {
            MetaDataObject metaValue = metaDataObject.metaObjectForProperty(propertyTokenizer.getNameAndIndex());
            if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
                return true;
            } else {
                return metaValue.hasGetter(propertyTokenizer.getNext());
            }
        } else {
            return false;
        }
    }

    @Override
    public MetaDataObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
        HashMap<String, Object> map = new HashMap<>();
        set(prop, map);
        return MetaDataObject.forObject(map, metaDataObject.getObjectFactory(),
                metaDataObject.getObjectWrapperFactory(), metaDataObject.getReflectorFactory());
    }

    @Override
    public boolean isCollection() {
        return false;
    }

    @Override
    public void add(Object element) {
        throw new UnsupportedOperationException();
    }

    @Override
    public <E> void addAll(List<E> element) {
        throw new UnsupportedOperationException();
    }

}
